<template>
  <view class="permission-container">
    <!-- 顶部栏 -->
    <view class="header">
      <view class="back-btn" @tap="goBack">
        <text class="back-icon">←</text>
      </view>
      <text class="header-title">权限管理</text>
      <view class="header-right"></view>
    </view>
    
    <!-- 权限说明 -->
    <view class="permission-intro">
      <text class="intro-text">个人助手需要以下权限以提供更好的服务体验</text>
    </view>
    
    <!-- 权限列表 -->
    <view class="permissions-section">
      <!-- 位置权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">📍</text>
            <text class="permission-name">位置信息</text>
          </view>
          <switch :checked="locationPermissionEnabled" @change="toggleLocationPermission"/>
        </view>
        <text class="permission-description">用于提供基于位置的服务，如附近的人和地点推荐</text>
      </view>
      
      <!-- 相机权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">📷</text>
            <text class="permission-name">相机</text>
          </view>
          <switch :checked="cameraPermissionEnabled" @change="toggleCameraPermission"/>
        </view>
        <text class="permission-description">用于扫描二维码、拍摄照片和视频</text>
      </view>
      
      <!-- 相册权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">🖼️</text>
            <text class="permission-name">相册</text>
          </view>
          <switch :checked="albumPermissionEnabled" @change="toggleAlbumPermission"/>
        </view>
        <text class="permission-description">用于访问和管理设备中的照片和视频</text>
      </view>
      
      <!-- 麦克风权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">🎤</text>
            <text class="permission-name">麦克风</text>
          </view>
          <switch :checked="microphonePermissionEnabled" @change="toggleMicrophonePermission"/>
        </view>
        <text class="permission-description">用于语音输入、语音识别和录音功能</text>
      </view>
      
      <!-- 通知权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">🔔</text>
            <text class="permission-name">通知</text>
          </view>
          <switch :checked="notificationPermissionEnabled" @change="toggleNotificationPermission"/>
        </view>
        <text class="permission-description">用于接收应用消息、提醒和推送通知</text>
      </view>
      
      <!-- 存储权限 -->
      <view class="permission-item">
        <view class="permission-header">
          <view class="permission-info">
            <text class="permission-icon">💾</text>
            <text class="permission-name">存储</text>
          </view>
          <switch :checked="storagePermissionEnabled" @change="toggleStoragePermission"/>
        </view>
        <text class="permission-description">用于保存应用数据、缓存文件和用户资料</text>
      </view>
    </view>
    
    <!-- 权限管理提示 -->
    <view class="permission-tips">
      <text class="tips-title">权限管理说明</text>
      <text class="tips-content">
        1. 您可以随时在"设置-隐私-权限管理"中修改应用权限。
        
        2. 关闭某些权限可能会影响应用的部分功能正常使用。
        
        3. 我们会严格保护您的隐私，不会滥用您授予的权限。
      </text>
    </view>
    
    <!-- 重置权限按钮 -->
    <view class="reset-container">
      <button class="reset-btn" @tap="resetAllPermissions">重置所有权限</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 模拟权限状态
      locationPermissionEnabled: true,
      cameraPermissionEnabled: true,
      albumPermissionEnabled: true,
      microphonePermissionEnabled: false,
      notificationPermissionEnabled: true,
      storagePermissionEnabled: true
    };
  },
  onLoad() {
    // 加载权限状态
    this.loadPermissions();
  },
  methods: {
    // 加载权限状态
    loadPermissions() {
      // 模拟从本地存储加载权限状态
      // 实际项目中应该调用微信的权限检查API
      const permissionKeys = [
        'locationPermissionEnabled',
        'cameraPermissionEnabled',
        'albumPermissionEnabled',
        'microphonePermissionEnabled',
        'notificationPermissionEnabled',
        'storagePermissionEnabled'
      ];
      
      permissionKeys.forEach(key => {
        const savedValue = uni.getStorageSync(key);
        if (savedValue !== '') {
          this[key] = JSON.parse(savedValue);
        }
      });
    },
    
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 请求权限
    requestPermission(permissionType, permissionName) {
      // 模拟请求权限
      // 实际项目中应该调用微信的权限请求API
      uni.showLoading({
        title: '请求权限中...'
      });
      
      setTimeout(() => {
        uni.hideLoading();
        
        // 模拟用户授权结果（80%概率授权）
        const isAuthorized = Math.random() < 0.8;
        
        if (isAuthorized) {
          this[permissionType] = true;
          this.savePermission(permissionType, true);
          
          uni.showToast({
            title: `已获得${permissionName}权限`,
            icon: 'success'
          });
        } else {
          this[permissionType] = false;
          this.savePermission(permissionType, false);
          
          uni.showModal({
            title: '权限被拒绝',
            content: `您拒绝了${permissionName}权限，某些功能可能无法正常使用。\n\n请在设备设置中允许应用访问该权限。`,
            showCancel: false,
            confirmText: '我知道了'
          });
        }
      }, 1500);
    },
    
    // 保存权限状态
    savePermission(permissionType, value) {
      uni.setStorageSync(permissionType, JSON.stringify(value));
    },
    
    // 切换位置权限
    toggleLocationPermission(e) {
      const isEnabled = e.detail.value;
      if (isEnabled) {
        this.requestPermission('locationPermissionEnabled', '位置信息');
      } else {
        this.locationPermissionEnabled = false;
        this.savePermission('locationPermissionEnabled', false);
        
        uni.showToast({
          title: '已关闭位置信息权限',
          icon: 'none'
        });
      }
    },
    
    // 切换相机权限
    toggleCameraPermission(e) {
      const isEnabled = e.detail.value;
      if (isEnabled) {
        this.requestPermission('cameraPermissionEnabled', '相机');
      } else {
        this.cameraPermissionEnabled = false;
        this.savePermission('cameraPermissionEnabled', false);
        
        uni.showToast({
          title: '已关闭相机权限',
          icon: 'none'
        });
      }
    },
    
    // 切换相册权限
    toggleAlbumPermission(e) {
      const isEnabled = e.detail.value;
      if (isEnabled) {
        this.requestPermission('albumPermissionEnabled', '相册');
      } else {
        this.albumPermissionEnabled = false;
        this.savePermission('albumPermissionEnabled', false);
        
        uni.showToast({
          title: '已关闭相册权限',
          icon: 'none'
        });
      }
    },
    
    // 切换麦克风权限
    toggleMicrophonePermission(e) {
      const isEnabled = e.detail.value;
      if (isEnabled) {
        this.requestPermission('microphonePermissionEnabled', '麦克风');
      } else {
        this.microphonePermissionEnabled = false;
        this.savePermission('microphonePermissionEnabled', false);
        
        uni.showToast({
          title: '已关闭麦克风权限',
          icon: 'none'
        });
      }
    },
    
    // 切换通知权限
    toggleNotificationPermission(e) {
      const isEnabled = e.detail.value;
      this.notificationPermissionEnabled = isEnabled;
      this.savePermission('notificationPermissionEnabled', isEnabled);
      
      uni.showToast({
        title: isEnabled ? '已开启通知权限' : '已关闭通知权限',
        icon: 'none'
      });
    },
    
    // 切换存储权限
    toggleStoragePermission(e) {
      const isEnabled = e.detail.value;
      this.storagePermissionEnabled = isEnabled;
      this.savePermission('storagePermissionEnabled', isEnabled);
      
      uni.showToast({
        title: isEnabled ? '已开启存储权限' : '已关闭存储权限',
        icon: 'none'
      });
    },
    
    // 重置所有权限
    resetAllPermissions() {
      uni.showModal({
        title: '重置权限',
        content: '确定要重置所有权限设置吗？\n\n这将恢复默认权限设置，但不会自动请求已拒绝的权限。',
        confirmText: '确定',
        confirmColor: '#ff4d4f',
        success: (res) => {
          if (res.confirm) {
            // 重置权限状态
            this.locationPermissionEnabled = true;
            this.cameraPermissionEnabled = true;
            this.albumPermissionEnabled = true;
            this.microphonePermissionEnabled = false;
            this.notificationPermissionEnabled = true;
            this.storagePermissionEnabled = true;
            
            // 保存重置后的权限状态
            const permissionKeys = [
              'locationPermissionEnabled',
              'cameraPermissionEnabled',
              'albumPermissionEnabled',
              'microphonePermissionEnabled',
              'notificationPermissionEnabled',
              'storagePermissionEnabled'
            ];
            
            permissionKeys.forEach(key => {
              this.savePermission(key, this[key]);
            });
            
            uni.showToast({
              title: '权限已重置',
              icon: 'success'
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.permission-container {
  height: 100vh;
  background-color: #f8f9fc;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 顶部栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 40rpx;
  background-color: #fff;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.back-btn {
  padding: 10rpx;
}

.back-icon {
  font-size: 32rpx;
  color: #333;
}

.header-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
}

.header-right {
  width: 50rpx;
}

/* 权限说明 */
.permission-intro {
  padding: 30rpx 40rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
}

.intro-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
}

/* 权限列表 */
.permissions-section {
  background-color: #fff;
  margin-bottom: 20rpx;
}

.permission-item {
  padding: 30rpx 40rpx;
  border-bottom: 1rpx solid #eee;
}

.permission-item:last-child {
  border-bottom: none;
}

.permission-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15rpx;
}

.permission-info {
  display: flex;
  align-items: center;
}

.permission-icon {
  font-size: 36rpx;
  margin-right: 20rpx;
}

.permission-name {
  font-size: 32rpx;
  color: #333;
}

.permission-description {
  font-size: 26rpx;
  color: #999;
  line-height: 1.6;
}

/* 权限管理提示 */
.permission-tips {
  padding: 30rpx 40rpx;
  background-color: #fff;
  margin-bottom: 40rpx;
}

.tips-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.tips-content {
  font-size: 26rpx;
  color: #666;
  line-height: 1.8;
  white-space: pre-line;
}

/* 重置按钮 */
.reset-container {
  padding: 0 40rpx 40rpx;
}

.reset-btn {
  background-color: #ff4d4f;
  color: #fff;
  font-size: 32rpx;
  border-radius: 40rpx;
  padding: 20rpx 0;
}

.reset-btn:active {
  background-color: #ff7875;
}

/* switch 样式 */
switch {
  transform: scale(0.8);
}
</style>